package com.web.provider;

import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.jdbc.SQL;

import java.util.Map;

public class UserProvider {

    public String queryUsersByParamsSql(Map<String, Object> para) {
        return new SQL() {
            {
                SELECT("id,name");
                FROM("user");
                WHERE("state=1");
                if (StringUtils.isNoneBlank((CharSequence) para.get("name"))) {
                    WHERE("name like concat('%',#{name},'%')");
                }
            }
        }.toString();
    }

    /**
     * https://mybatis.org/mybatis-3/zh/statement-builders.html#
     *
     * @param para
     */
    public String queryUsersByParamsSql2(Map<String, Object> para) {
        return new SQL() {
            {
                SELECT("user.*,user_info.address").
                        FROM("user").
                        LEFT_OUTER_JOIN("user_info on user.id = user_info.user_id").
                        ORDER_BY("user.id", "user_info.address desc").
                        WHERE("user.state=1 and user.deleted=0");
                // WHERE("user.state=1").WHERE("user.deleted=0");
                if (StringUtils.isNoneBlank((CharSequence) para.get("name"))) {
                    WHERE("user.name like concat('%',#{name},'%')");
                }
            }
        }.toString();
    }

}
